package com.xiaoger.async.async;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;

import java.lang.reflect.Method;

/**
 * 异步方法异常拦截器
 *
 * @author xjx
 * @date 2022/5/11 16:21
 */
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {

    private static final Logger log = LoggerFactory.getLogger(AsyncExceptionHandler.class);

    /**
     * 处理从异步方法抛出的异常
     *
     * @param ex
     * @param method
     * @param params
     */
    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        log.error("异步方法执行出现了异常，异常内容为：" + ex);
        //可以获取到指定异常
        if (ex instanceof AsyncException) {
            AsyncException asyncException = (AsyncException) ex;
            log.error("异常消息为：{}", asyncException.getMessage());
        }
        log.info("异步调用出错的方法为:{}", method);
        log.info("异步调用出错的方法的参数为:{}", params);
    }
}
